<?php
/**
 * Created by PhpStorm.
 * User: Machenike
 * Date: 2019/12/14
 * Time: 9:48
 */

namespace App\Service\Alipay;


use App\Base\BaseConfig;

require(__DIR__ . "/../../../BaseExt/AliPay/aop/AopClient.php");
require(__DIR__ . "/../../../BaseExt/AliPay/aop/request/AlipayTradeAppPayRequest.php");
//require(__DIR__ . "/../../../BaseExt/AliPay/aop/request/AlipayUserCertifyOpenCertifyRequest.php");
//require(__DIR__ . "/../../../BaseExt/AliPay/aop/request/AlipayUserCertifyOpenQueryRequest.php");
class Alipay
{
//    private $rsaPrivateKey = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCV/mHDGBILcJFXsZQfs+OzVSZQTGKqVjX5GCQWr+H9VQXSA1L1zqqNs+PvoN0G6yepEB1CM1KiPskhvfS0faNiwAydFd6ZrPy/pbGQjbmvt97QM7Jd5w9D94czz1zXkh+mHw3LXj94VC9z9Qpe6I6vd1Poelm2DjWQGEtC/YNz/hr58/WLA40Z/hyILAzarhA0Xvy3/WwWG1Fxv7ERlE1nJZTTJdO4I94+nTSGKvaCIwWarXImloyj/WMy+4ZTmm2ppRDfxe7vxtKJuXcyUwcMXgIDr0sgjUu//QOj+ABDONwKZHMsozw8sOx8xn3toqXsmx3iNnr5G3zNmyv5i7k5AgMBAAECggEAGDUa8BKlbuU2/9EwF8G/oQTADzuUvOr3AQGtaV/veCRNsSxKKlO1YPNEeTscEO7I0tc/8Zw43OiHK4Ptx2dzhA98ual66JBrpj6Gns7QxhRFyJJfWhpdc1myn2D7GeuCYhwm7MiEM9ZaZjuI9fJQfO8kQqPkcKZwbJYPJ3ukyIZd4snHVtkXe+1c87GFiYlVs/oAQwL+Rfq56IFoVWE5aN5EHrfUFDBpYpnPuY/aPfy1NYccrVGCmc7xoEzrz4+CPgKfC64UxBq8HIHCUL98UiyWQagST5spmy0ntY/cobyeQv3NvFGgaGiKcT1gjuACLaIVHGeBm1BMOkR/QEunkQKBgQDMmdjTstjZl1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5x0ZlpzawWG3pUGoX7i9knsvGSv/pPe/tS91Yee8mSTss2fwuFgUnMGlJBW7bEoubvTKjBBAhKEtaOyjENqUCabTb+MeYXFjbpl2FQ7OjXV5X0yhRc7sdkZh308wvKXnlaIFloo4n9jCrI+462x3Pw/LilxITwrX6PfsaPZKTh6D5F95FR2P+82UFRnxbogP+u2QIDAQAB';
//    private $gatewayUrl = "https://openapi.alipay.com/gateway.do";
//    private $appId = "2018041002531952";

//    private $rsaPrivateKey = "MIIEowIBAAKCAQEAslHKme2Mx2nr3TN+CkOSIlY2K1QbzZ3x+b70ksQEvCCjdgOG8wpYCy1CntPSyJwr1yfiFV4B8EJqwpApPNos28E4B8kcuSkPXrqNuQPA0SCFFKDnjnZhPBZm6XQYi5M6tg1UxOhU+EsRrC2chDuaDfpvUMHnX9eFBgQpqP0LA5/rdDPsbY7XQ+r8gmW504u+eDE+naech33WSWdQrgEcvgEX4rRwpLkdU/fZwvm/5EoUtKntJipCRsps2MnuQYiURst502yY3YBHx+dm6s8hKVrRJ9CSgrtQt8TGuuwIjIzxbU5MDOXB2yDbkGXXfqhby6m5wXcaBShtntGgOBJv/QIDAQABAoIBAFL9eAJdqWhhppFDVsSmIAv0/a76rRnsCWMHeUIZEm8P7aOODq9sP/+rp9bNlMsiy9CN0k3Je7N65b75YwfcLoZ0DYSqF8EhF/Mpda4GziVGMkmgfUQzWhuh/XFnQ8kp02hJGguuaWVJYsc0TyTHrHIW/Nmj9PAJGPLs2wq4AflEGa2N/3C5VXVccwHvZ5dLW/4MoAI0rqqN2MChLHvcPX+pwKIyv0oKtdj2uIrMiiMsa8cp+JIi00+JGitGll0m22+kZqRnofRBWT35/vha5iecU4SUxK1sze7pMRmRCTCqrDc4iMy1fzmgLEHPQE4hEjT2ppoGFyR3Rwr4H3Wg7+ECgYEA/DTxIaUnWGk+o5sdehotcKqYVtlYq1xr1WOcXkFq6pEnC8Mt52saMt24YkP/oVh0MrnPdkOREYOPh3I06PGGhUvVKgdIfPFXP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16iLnTl/0JkoVF2sjY5dPlohV2QIDAQAB';

//    正式环境
    private $gatewayUrl = "https://openapi.alipay.com/gateway.do";
    private $appId = "2021001144648176";
//    private $appId = "2021001144635033";
//    沙箱环境
//    private $gatewayUrl = "https://openapi.alipaydev.com/gateway.do";
//    private $appId = "2021001144648176";

    public function createOrder($sn,$price,$type = 0,$info = "订单支付",$cbStr = "")
    {
//        $sn = "sn001";
//        $price = 1;

        $aop = new \AopClient();
        $aop->gatewayUrl = $this->gatewayUrl;
        $aop->appId = $this->appId;
        //        开发者私钥
        $aop->rsaPrivateKey = $this->rsaPrivateKey;
        $aop->format = "json";
        $aop->charset = "UTF-8";
        $aop->signType = "RSA2";
        //支付宝公钥
        $aop->alipayrsaPublicKey = $this->alipayrsaPublicKey;
        //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay
        $request = new \AlipayTradeAppPayRequest();
        //SDK已经封装掉了公共参数，这里只需要传入业务参数
        $price = $price / 100;
        $bizcontent = "{\"body\":\"用户正在支付订单\","
            . "\"subject\": \"$info\","//这一行是订单支付的信息
            . "\"out_trade_no\": \"$sn\","
            . "\"passback_params\": \"$cbStr\","
            . "\"timeout_express\": \"30m\","
            . "\"total_amount\": \"$price\","
            . "\"product_code\":\"QUICK_MSECURITY_PAY\""
            . "}";
        // $request->setNotifyUrl("http://edu.sano100.com/app_dev.php/money/charge/77a90331fb27755e0361babcfd4f2264");
//        $request->setNotifyUrl(BaseConfig::__BASE_URL__."/pay/cb?$cbStr");
        $request->setNotifyUrl(BaseConfig::__BASE_URL__."/pay/cb/ali");
        $request->setBizContent($bizcontent);
        //这里和普通的接口调用不同，使用的是sdkExecute
        $response = $aop->sdkExecute($request);
        //htmlspecialchars是为了输出到页面时防止被浏览器将关键参数html转义，实际打印到日志以及http传输不会有这个问题
        return   ($response);//就是orderString 可以直接给客户端请求，无需再做处理。
    }
}
